﻿Imports BUS, DTO
Public Class frmThanhLySach
    Dim dsSach As New List(Of SachDTO)
    Dim dsNhanVien As New List(Of NhanVienDTO)
    Dim dsLyDoThanhLy As New List(Of LyDoThanhLySachDTO)

    Private Sub frmThanhLySach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sachBus As New BUS.SachBUS()
        Dim nhanvienBus As New NhanVienBUS()
        Dim lydothanhlyBus As New LyDoThanhLySachBUS

        dsSach = sachBus.LayDSSach()
        dsNhanVien = nhanvienBus.LayDSNhanVien_ThuThu()
        dsLyDoThanhLy = lydothanhlyBus.LayDSLyDoThanhLySach()

        LookUpEdit_NhanVienGhiNhan.Properties.DataSource = dsNhanVien
        LookUpEdit_NhanVienGhiNhan.Properties.ValueMember = "MaNhanVien"
        LookUpEdit_NhanVienGhiNhan.Properties.DisplayMember = "HoTen"
        LookUpEdit_NhanVienGhiNhan.EditValue = 0

        LookUpEdit_Sach.Properties.DataSource = dsSach
        LookUpEdit_Sach.Properties.ValueMember = "MaSach"
        LookUpEdit_Sach.Properties.DisplayMember = "TenSach"
        LookUpEdit_Sach.EditValue = 0

        LookUpEdit_LyDoThanhLy.Properties.DataSource = dsLyDoThanhLy
        LookUpEdit_LyDoThanhLy.Properties.ValueMember = "MaLyDoThanhLySach"
        LookUpEdit_LyDoThanhLy.Properties.DisplayMember = "TenLyDoThanhLySach"
        LookUpEdit_LyDoThanhLy.EditValue = 0

        DateTimePicker_NgayGhiNhan.Value = DateTime.Now()
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Dim s As New SachDTO()
        For Each item In dsSach
            If item.MaSach = LookUpEdit_Sach.EditValue Then
                s = item
                Exit For
            End If
        Next
        _DataGridView_DanhSachSach.Rows.Add(s.MaSach, s.TenSach, s.MaTheLoai, s.MaTacGia, s.NamXuatBan, s.MaNhaSanXuat, s.NgayNhap, s.TriGia, s.MaTinhTrang)
    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        Dim i As Integer = _DataGridView_DanhSachSach.CurrentRow.Index
        _DataGridView_DanhSachSach.Rows.RemoveAt(i)
    End Sub

    Private Sub SimpleButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton5.Click
        Dim str As String = String.Format("{0:dd/MM/yyyy}", DateTimePicker_NgayGhiNhan.Value)
        Dim tlsBus As New ThanhLySachBUS
        Dim slTLS As List(Of ThanhLySachDTO) = tlsBus.LayDanhSachThanhLySach()
        Dim sl As Integer = (slTLS.Count + 1)
        Dim sql As String = "insert into THANHLYSACH(MaNhanVien,NgayThanhLy,MaChiTietThanhLy) values ( "
        sql = sql & LookUpEdit_NhanVienGhiNhan.EditValue.ToString() & ", '" & str & "', " & sl.ToString() & ")"

        Dim kq As Integer = tlsBus.ThemThanhLaySach(sql)
        'MessageBox.Show(sql)

        If kq <> 1 Then
            MessageBox.Show("Thêm thanh lý sách thất bại !")
            Return
        End If

        Dim ctTLSBus As New ChiTietThanhLySachBUS
        Dim dong As Integer = _DataGridView_DanhSachSach.Rows.Count - 2
        For index As Integer = 0 To dong
            sql = "insert into CHITIETTHANHLYSACH (MaChiTietThanhLySach,MaSach,MaLyDoThanhLy)Values ( "
            sql = sql & sl.ToString() & "," & _DataGridView_DanhSachSach.Item(0, index).Value.ToString() & ", " & LookUpEdit_LyDoThanhLy.EditValue.ToString() & ")"
            'MessageBox.Show(sql)
            kq = ctTLSBus.ThemChiTietThanhLySach(sql)

            If kq <> 1 Then
                MessageBox.Show("Thêm chi tiết thanh lý sách thất bại !")
                Return
            End If
        Next
        MessageBox.Show("Thanh lý sách thành công !")
    End Sub
End Class